/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 *
 * @author Kvest
 */
public class Utils 
{
    public static int byteToInt(byte[] val)
    {
        int result = 0;
        long bitValue = 1;
        for (int i = 0; i < val.length; ++i)
        {
            for (int j = 1; j <= 128; j *= 2)
            {
                if ((val[i] & j) == j)
                {
                    result |= bitValue;
                }

                bitValue *= 2;
            }
        }

        return result;
    }
    
    public static byte boolToByte(boolean val)//������� �������� �� boolean � byte
    {
        if (val)
        {
            return 0x01;
        }
        else
        {
            return 0x00;
        }

    }

    public static byte[] int16ToByte(int val)//������� �������� �� int16 � ������ byte
    {
        byte t;
        int temp;
        byte[] result;
        result = new byte[2];
        Integer i = new Integer(val);
        t = i.byteValue();
        result[0] = t;
        temp = i.intValue();
        temp = temp>>8;
        i = new Integer(temp);
        t = i.byteValue();
        result[1] = t;
        System.gc();
        return result;
    }
    
    private static long byteToLong(byte[] val)
    {
        long result = 0;
        long bitValue = 1;
        for (int i = 0; i < val.length; ++i)
        {
            for (int j = 1; j <= 128; j *= 2)
            {
                if ((val[i] & j) == j)
                {
                    result |= bitValue;
                }

                bitValue = bitValue << 1;
            }
        }

        return result;
    }
    
    public static double byteToDecimal(byte[] val)
    {
            double k,temp;
            String tempStr;
            int exp;
            tempStr = "";
            temp = 0;
            //��������� � �������� ���
            for(int i = 0;i<val.length;i++)
            {
                    String substr;
                    substr = Integer.toBinaryString(val[i]);
                    if (substr.length()>8)
                    {
                            substr = substr.substring(substr.length()-8);
                    }
                    int delta = 8 - substr.length();
                    for (int j=0;j<delta;j++)
                    {
                            substr = '0'+substr;
                    }
                    tempStr =  substr+tempStr;
            }
            k = 1;
            //�������� ����� �����
            for(int i=tempStr.length()-1;i>=32;i--)
            {
                    if (tempStr.charAt(i)=='1')
                    {
                            temp += k;
                    }
                    k *=2;
            }
            //������� �����
            k = 1;
            exp = 0;
            for (int i=15;i>=8;i--)
            {
                    if (tempStr.charAt(i)=='1')
                    {
                            exp += k;
                    }
                    k *=2;
            }
            if (exp>28)
            {
                    exp = 28;
            }
            k = 1;
            for (int i=1;i<=exp;i++)
            {
                    k *= 10;
            }
            temp = temp / k;
            //���������� ����
            if (tempStr.charAt(0)=='1')
            {
                    temp = -temp;
            }
            return temp;
    }
    
    public static long byteToDate(byte[] val)
    {
        long l;
        l = byteToLong(val);
        //�������� � �������������
        l = l/10000;
        //���� ���������� ���������� ����
        l -= 62135596800000l;
        return l;
    }
    
    public static String dateToString(long ticks)
    {
        Date d = new Date(ticks);
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));//getDefault());
        calendar.setTime(d);
        String temp = "";
        if (calendar.get(Calendar.DAY_OF_MONTH)<10)
        {
                temp += "0"+Integer.toString (calendar.get(Calendar.DAY_OF_MONTH));
        }
        else
        {
                temp += Integer.toString (calendar.get(Calendar.DAY_OF_MONTH));
        }
        if ((calendar.get(Calendar.MONTH)+1)<10)
        {
                temp +=".0" + Integer.toString (calendar.get(Calendar.MONTH)+1);
        }
        else
        {
                temp +="." + Integer.toString (calendar.get(Calendar.MONTH)+1);
        }
        temp +="." + Integer.toString (calendar.get(Calendar.YEAR));
        temp +=" " + Integer.toString (calendar.get(Calendar.HOUR_OF_DAY));
        if (calendar.get(Calendar.MINUTE)<10)
        {
                temp +=":0" + Integer.toString (calendar.get(Calendar.MINUTE));
        }
        else
        {
                temp +=":" + Integer.toString (calendar.get(Calendar.MINUTE));
        }
        return temp;
    }

    public static long stringToDate(String value)
    {
        if (!isInvalidDate(value))
        {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date(0));

            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(value.substring(0,2)));
            calendar.set(Calendar.MONTH, Integer.parseInt(value.substring(3,5)));
            calendar.set(Calendar.YEAR, Integer.parseInt(value.substring(6,10)));
            
            return calendar.getTime().getTime();
        }
        else
        {
            return -1;
        }
    }

    public static String dateToDateString(long ticks)
    {
        Date d = new Date(ticks);
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));//getDefault());
        calendar.setTime(d);
        String temp = "";
        if (calendar.get(Calendar.DAY_OF_MONTH)<10)
        {
                temp += "0"+Integer.toString (calendar.get(Calendar.DAY_OF_MONTH));
        }
        else
        {
                temp += Integer.toString (calendar.get(Calendar.DAY_OF_MONTH));
        }
        if ((calendar.get(Calendar.MONTH)+1)<10)
        {
                temp +=".0" + Integer.toString (calendar.get(Calendar.MONTH)+1);
        }
        else
        {
                temp +="." + Integer.toString (calendar.get(Calendar.MONTH)+1);
        }
        temp +="." + Integer.toString (calendar.get(Calendar.YEAR));
        return temp;
    }

    public static String dateToTimeString(long ticks)
    {
        Date d = new Date(ticks);
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));//getDefault());
        calendar.setTime(d);
        String temp = "";
        temp +=Integer.toString (calendar.get(Calendar.HOUR_OF_DAY));
        if (calendar.get(Calendar.MINUTE)<10)
        {
                temp +=":0" + Integer.toString (calendar.get(Calendar.MINUTE));
        }
        else
        {
                temp +=":" + Integer.toString (calendar.get(Calendar.MINUTE));
        }
        return temp;
    }

    public static byte[] int32ToByte(int val)//������� �������� �� int32 � ������ byte
    {
        byte t;
        int temp;
        byte[] result;
        result = new byte[4];
        Integer i = new Integer(val);
        t = i.byteValue();
        result[0] = t;
        temp = i.intValue();
        temp = temp>>8;
        i = new Integer(temp);
        t = i.byteValue();
        result[1] = t;
        temp = i.intValue();
        temp = temp>>8;
        i = new Integer(temp);
        t = i.byteValue();
        result[2] = t;
        temp = i.intValue();
        temp = temp>>8;
        i = new Integer(temp);
        t = i.byteValue();
        result[3] = t;
        System.gc();
        return result;
    }

    public static byte[] longToByte(long val)//������� �������� �� long � ������ byte
    {
        byte[] result,temp;
        result = new byte[8];
        int intVal;

        intVal = (int)val;
        temp = int32ToByte(intVal);
        for (int i=0;i<4;i++)
        {
            result[i] = temp[i];
        }

        val = val>>32;
        intVal = (int)val;
        temp = int32ToByte(intVal);
        for (int i=0;i<4;i++)
        {
            result[i+4] = temp[i];
        }

        return result;
    }
    
    public static boolean isInvalidDate(String value)
    {
        value = value.trim();
        return ((value.length() != 10) ||
                (value.charAt(2) != '.') ||
                (value.charAt(5) != '.') ||
                (!Character.isDigit(value.charAt(0))) ||
                (!Character.isDigit(value.charAt(1))) ||
                (!Character.isDigit(value.charAt(3))) ||
                (!Character.isDigit(value.charAt(4))) ||
                (!Character.isDigit(value.charAt(6))) ||
                (!Character.isDigit(value.charAt(7))) ||
                (!Character.isDigit(value.charAt(8))) ||
                (!Character.isDigit(value.charAt(9))));
    }
}
